﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class NewController : MonoBehaviour {

    public static NewController Zouni;

    public Color purple; //紫色

    public Color Yanse;

    public SkinnedMeshRenderer footRenderer;
    public Mesh[] footMeshArray; // 定义数组存储Foot的Mesh网格
    private int footIndex = 0;

    public SkinnedMeshRenderer headRenderer;
    public Mesh[] headMeshArray;
    private int headIndex = 0;

    public SkinnedMeshRenderer handRenderer;
    public Mesh[] handMeshArray;
    private int handIndex = 0;

    public SkinnedMeshRenderer upbodyRenderer;
    public Mesh[] upbodyMeshArray;
    private int upbodyIndex = 0;

    public SkinnedMeshRenderer lowerbodyRenderer;
    public Mesh[] lowerbodyMeshArray;
    private int lowerbodyIndex = 0;

    void Awake()
    {
        // 单例模式
        Zouni = this;
    }

    void Start() 
    {

        // 在加载的时候，不销毁脚本所在的游戏对象
        DontDestroyOnLoad(this.gameObject);

        // 事件监听方法
        NewController awss = this;
        EventDelegate ZongClick = new EventDelegate(awss, "OnChangeFoot");
        GameObject.Find("FOOT").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss,"OnChangeHead");
        GameObject.Find("HEAD").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss, "OnChangeHand");
        GameObject.Find("HAND").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss, "OnChangeUpBody");
        GameObject.Find("UPBODY").GetComponent<UIButton>().onClick.Add(ZongClick);
  //      GameObject.Find("LOWERBODY").GetComponent<UIButton>().onClick.Add(ZongClick); 这句可以绑定在两个按钮上

        ZongClick = new EventDelegate(awss, "OnChangeLowerBody");
        GameObject.Find("LOWERBODY").GetComponent<UIButton>().onClick.Add(ZongClick);

        // 颜色
        ZongClick = new EventDelegate(awss, "OnChangeBlue");
        GameObject.Find("Blue").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss,"OnChangeRed");
        GameObject.Find("Red").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss, "OnChangeCyan");
        GameObject.Find("Cyan").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss, "OnChangeGreen");
        GameObject.Find("Green").GetComponent<UIButton>().onClick.Add(ZongClick);

        ZongClick = new EventDelegate(awss, "OnChangePurple");
        GameObject.Find("Purple").GetComponent<UIButton>().onClick.Add(ZongClick);

        // 关卡
        ZongClick = new EventDelegate(awss, "Play");
        GameObject.Find("PLAY").GetComponent<UIButton>().onClick.Add(ZongClick);
    }

    public void OnChangeFoot()
    {
        footIndex++;
        footIndex %= footMeshArray.Length;
        footRenderer.sharedMesh = footMeshArray[footIndex];

    }

    public void OnChangeHead()
    {
        headIndex++;
        headIndex %= headMeshArray.Length;
        headRenderer.sharedMesh = headMeshArray[headIndex];
    }

    public void OnChangeHand()
    {
        handIndex++;
        handIndex %= handMeshArray.Length;
        handRenderer.sharedMesh = handMeshArray[handIndex];
    }

    public void OnChangeUpBody()
    {
        upbodyIndex++;
        upbodyIndex %= upbodyMeshArray.Length;
        upbodyRenderer.sharedMesh = upbodyMeshArray[upbodyIndex];
    }

    public void OnChangeLowerBody()
    {
        lowerbodyIndex++;
        lowerbodyIndex %= lowerbodyMeshArray.Length;
        lowerbodyRenderer.sharedMesh = lowerbodyMeshArray[lowerbodyIndex];
    }



    // 颜色
    public void OnChangeBlue()
    {
        ChangeColor(Color.blue);
    }

    public void OnChangeCyan()
    {
        ChangeColor(Color.cyan);
    }

    public void OnChangeGreen()
    {
        ChangeColor(Color.green);
    }

    public void OnChangePurple()
    {
        ChangeColor(purple);
    }

    public void OnChangeRed()
    {
        ChangeColor(Color.red);
    }

    // 更换颜色的步骤统一在本方法中
    void ChangeColor(Color ab)
    {
        GameObject.Find("AHead").GetComponent<SkinnedMeshRenderer>().material.color = ab;
        GameObject.Find("BUpperBody").GetComponent<SkinnedMeshRenderer>().material.color = ab;
        GameObject.Find("CLowerBody").GetComponent<SkinnedMeshRenderer>().material.color = ab;
        GameObject.Find("DHand").GetComponent<SkinnedMeshRenderer>().material.color = ab;
        GameObject.Find("EFoot").GetComponent<SkinnedMeshRenderer>().material.color = ab;
        Yanse = ab;
    }

    public void Save()
    {

        PlayerPrefs.SetInt("HeadIndex", headIndex);
        PlayerPrefs.SetInt("HandIndex", handIndex);
        PlayerPrefs.SetInt("FootIndex", footIndex);
        PlayerPrefs.SetInt("UpBodyIndex", upbodyIndex);
        PlayerPrefs.SetInt("LowerIndex", lowerbodyIndex);

    }

    public void Play()
    {
        Save();
      //  SceneManager.LoadScene("L1");
        Application.LoadLevel("L1");
    }
}
